package com.qiaofang.collectmanagebff.service.util.threadTask;

/**
 * @auth liyi
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.*;

@Component
public class ThreadFrame {

    Logger logger = LoggerFactory.getLogger(ThreadFrame.class);

    ExecutorService cachedThreadPool = new ThreadPoolExecutor(5, 200,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024));

    private LinkedBlockingQueue queue  = new LinkedBlockingQueue<Integer>(1);

    public void addTask(Task task){
        //队列满时,一直阻塞等待空间
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    queue.put(task);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                }
        });
    }

    public ThreadFrame(){
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){e.printStackTrace();}
                    try {
                        //队列为空将一直等待
                        Task task = (Task) queue.take();
                        task.batchUpdateHouseAgent();
                    }catch (InterruptedException e){e.printStackTrace();}
                }
            }
        });
    }
}
